﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DBExperment
{
    public partial class MainForm : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        public DataTable BookTable=new DataTable();
        public Login login;

        public void FillBookTable(string sql)
        {
            BookTable = Data.FindBookBy(sql);
            BookTable.Columns.Add("选中 ", typeof(bool));
            BookTable.Columns[6].SetOrdinal(0);
            BookTable.Columns[1].ColumnName = "书号";
            BookTable.Columns[2].ColumnName = "书名";
            BookTable.Columns[3].ColumnName = "作者";
            BookTable.Columns[4].ColumnName = "出版社";
            BookTable.Columns[5].ColumnName = "类别";
            BookTable.Columns[6].ColumnName = "ISBN";

            for (int i = 0; i < BookTable.Rows.Count; i++)
            {
                BookTable.Rows[i][0] = false;
            }

            DGV_Book.DataSource = BookTable;
            DGV_Book.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            DGV_Book.Columns[0].FillWeight = 10;
            DGV_Book.Columns[1].FillWeight = 10;
            DGV_Book.Columns[2].FillWeight = 30;
            DGV_Book.Columns[3].FillWeight = 20;
            DGV_Book.Columns[4].FillWeight = 20;
            DGV_Book.Columns[5].FillWeight = 20;
            DGV_Book.Columns[6].FillWeight = 20;
            
        }

        public MainForm(Login lg)
        {
            login = lg;
            InitializeComponent();
            FillBookTable(@"SELECT * From tb_Book");
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            AddBookForm addBookForm=new AddBookForm();
            addBookForm.ShowDialog();
            FillBookTable(@"SELECT * From tb_Book");
        }

        private void btn_Find_Click(object sender, EventArgs e)
        {
            string sql="";
            if(rad_BName.Checked)
            {
                sql = "SELECT * From tb_Book WHERE BName=" + "'" + txt_Input.Text + "'";
            }
            if (rad_Author.Checked)
            {
                sql = "SELECT * From tb_Book WHERE Author=" + "'" + txt_Input.Text + "'";
            }
            if (rad_Publisher.Checked)
            {
                sql = "SELECT * From tb_Book WHERE Publisher=" + "'" + txt_Input.Text + "'";
            }
            if (rad_Type.Checked)
            {
                sql = "SELECT * From tb_Book WHERE Type=" + "'" + txt_Input.Text + "'";
            }
            if (rad_ISBN.Checked)
            {
                sql = "SELECT * From tb_Book WHERE ISBN=" + "'" + txt_Input.Text + "'";
            }
            FillBookTable(sql);
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (Convert.ToBoolean(MessageBox.Show("确定删除记录", "确认删除", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)))
            {
                for (int i = 0; i < BookTable.Rows.Count; i++)
                {
                    if ((bool) BookTable.Rows[i][0])
                    {
                        //MessageBox.Show("DELETE * FROM tb_Book WHERE BID=" + BookTable.Rows[i][1]);
                        Data.AddData("DELETE * FROM tb_Book WHERE BID=" + BookTable.Rows[i][1]);
                        BookTable.Rows.Remove(BookTable.Rows[i]);                        
                    }
                }
            }
        }

        private void DGV_Book_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int RIndex = e.RowIndex;
            if (RIndex > -1)
            {
                BookTable.Rows[RIndex][0] = !Convert.ToBoolean(BookTable.Rows[RIndex][0]);
            }
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            login.Close();
        }

        private void btn_AddBookList_Click(object sender, EventArgs e)
        {
            int RIndex = DGV_Book.CurrentRow.Index;            
            AddBookListForm addBookListForm = new AddBookListForm(BookTable.Rows[RIndex][1].ToString(),BookTable.Rows[RIndex][2].ToString());
            addBookListForm.ShowDialog();
        }

        private void TabControl_SelectedPageChanged(object sender, EventArgs e)
        {
            switch (TabControl.SelectedIndex)
            {
                case 0:
                    FillBookTable(@"SELECT * From tb_Book");
                    break;
                case 1:
                    bookListPanel.FillBookTable("SELECT * From tb_BookList");
                    break;
                case 2:
                    break;
                case 3:
                    break;
            }
            

        }
    }
}
